package de.ars.client.items.elements;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;


import com.sun.lwuit.Font;

import de.ars.client.Main;
import de.ars.client.datahandling.PropertyReader;
import de.ars.client.paintSystems.StringPainter;

public class ChooseItem extends AbstractItem
{
	//Input Variablen
	
	private int textWidth = 0;
	
    private int height;
    private int width; 

    private StringPainter stringPainter;
    private Font font = Main.getInstance().getFont( 0 );

    private Vector options;
    private int choosenOptionsIndex = 0;
    
    public ChooseItem( StringPainter stringPainter, String text, String key, Vector options )
    {
        this.stringPainter = stringPainter;
        this.options = options;
        this.key = key;
        height = font.getHeight()+2;
        setText(text);
    }

    public void setHeight( int height )
    {
        this.height = height;
    }

    public int getHeight()
    {
        return this.height;
    }
    
    public int getWidth()
    {
        return this.width;
    }

    public void setText( String text )
    {
        this.text = text;
        recalcWidth();
    }
    
    public void recalcWidth()
    {
    	textWidth = font.stringWidth(this.text);
        this.width = textWidth;
        if (this.options != null && this.options.size() > 0)
        	this.width += font.stringWidth(PropertyReader.getProperty( "label." + this.options.elementAt(choosenOptionsIndex).toString()));
        this.width += 6;
    }

    public void setTextWithoutWidth(String _text)
    {
    	this.text = _text;
    }
     
	public void keyPressed(int arg0)
	{
		switch (arg0)
		{
		case -3:
			this.choosenOptionsIndex--;
			if (this.choosenOptionsIndex < 0)
				this.choosenOptionsIndex = this.options.size()-1;
			recalcWidth();
			break;
		case -4:
			this.choosenOptionsIndex++;
			if (this.choosenOptionsIndex > this.options.size()-1)
				this.choosenOptionsIndex = 0;
			recalcWidth();
			break;
		}
	}

	public void paint(Graphics g, boolean selected)
	{
		if(selected)
    	{
	    	g.drawRect( 0, 0, width, this.height + 1 );
	        g.setColor( 0xffffffff );
	        g.fillRect( 1, 1, width - 1, this.height);
	        g.setColor( 0x00000000 );
    	}
        
		stringPainter.drawString( g, this.text , 4, (this.height+1)/2-this.font.getHeight()/2);
        
        if(this.options != null)
        {
        	this.stringPainter.drawString(g, PropertyReader.getProperty( "label." + this.options.elementAt(choosenOptionsIndex).toString()), this.textWidth + 4,	(this.height+1)/2-this.font.getHeight()/2);
        }
	}
	
	public String getValue()
	{
		return this.options.elementAt(this.choosenOptionsIndex).toString();
	}
	
	public void setValue(String _value)
	{
		for (int i = 0 ; i < this.options.size();i++)
		{
			if (_value.equals(this.options.elementAt(i)))
				this.choosenOptionsIndex = i;
		}
	}
}
